<?php 

include("helper/DBOperation.php");
$dbObj = new DBOperation();

include("helper/check_user_login.php");
checkUserIsLogin();

?>

<!DOCTYPE php PUBLIC "-//W3C//DTD Xphp 1.0 Transitional//EN" "http://www.w3.org/TR/xphp1/DTD/xphp1-transitional.dtd">

<php xmlns="http://www.w3.org/1999/xphp">
<head>
<meta name = "viewport" content = "width = device-width">

<meta name="apple-mobile-web-app-capable" content="yes" />

<title>HBMS - System</title>
<link href="HBMS_css.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
// Mobile Safari in standalone mode
if(("standalone" in window.navigator) && window.navigator.standalone){

// If you want to prevent remote links in standalone web apps opening Mobile Safari, change 'remotes' to true
var noddy, remotes = false;

document.addEventListener('click', function(event) {

noddy = event.target;

// Bubble up until we hit link or top php element. Warning: BODY element is not compulsory so better to stop on php
while(noddy.nodeName !== "A" && noddy.nodeName !== "php") {
noddy = noddy.parentNode;
}

if('href' in noddy && noddy.href.indexOf('http') !== -1 && (noddy.href.indexOf(document.location.host) !== -1 || remotes)) {
event.preventDefault();
document.location.href = noddy.href;
}

},false);
}

</script>


</head>

<body>

<div id="system_main">
	<div class="unit_a_content">
    
    	<div class="unit_a_top_row">
        	<div class="unit_a_cemusa_img"></div>
		<?php if($cookie['userslog']['is_admin']==1){ ?>
				<a href="admin/user_listing.php" style="text-decoration:none;color:white;"><img src="images/person.png" width="20px" height="20px" title="Add User"/></a>
			<?php }?>
            <div class="unita_top_text_part">
            	<div class="unita_top_txt1"><span>logged in as:</span> <?=$cookie['userslog']['display_name']?></div>
                <div class="unita_top_txt1"><span>last update:</span> 
		<?php if($lastLogUpdateTime = $dbObj->lastUpdatedLogTime()) { echo $lastLogUpdateTime; } else { echo "00:00:00"; } ?></div>
            </div>
        </div>
        <?php 
	$device_id = $_REQUEST['id'];
	$result_devicename = array();
	if($device_id) {
		$result_devicename = $dbObj->executeQuery("SELECT device_name FROM tbl_device where id=".$device_id , true);
	}


$strip_color = array();
$componentResult = $dbObj->getRecords("tbl_components", "", array("device_id"=>$device_id), "", "");

$total_components = mysql_num_rows($componentResult);
while($row_component = mysql_fetch_array($componentResult)) {

	$qry = "select * from tbl_component_state where component_id='".$row_component['id']."' order by id DESC limit 1";
	$result = mysql_query($qry);
	$total = mysql_num_rows($result);	
	$x = 0;
	$color_state = array();
	while($row=mysql_fetch_array($result))
		$color_state[] = $row['state'];
	if(isset($color_state[0])) {
		$strip_color[] = $color_state[0];	
	} else {
		$strip_color[] = "blue";
	}

}
//$barColor = "blue";
if(in_array("blue",$strip_color)){
	$barColor = "blue";
}elseif(in_array("red",$strip_color)){
	$barColor = "red";
}elseif(in_array("yellow",$strip_color)){
	$barColor = "yellow";
}elseif(in_array("green",$strip_color)){
	$barColor = "green";
}
	?>
        <div class="system_unit_row">
                <div class="system_top_home_bttn"><a href="unit_a.php">Home</a></div>
                <div class="system_unit_txt"><div class="bar" style="background:<?php echo $barColor; ?>"></div><?php if(isset($result_devicename['device_name'])) { 
			echo $result_devicename['device_name']; } ?></div>
               <!-- <div class="system_unit_icon"><a href="alert_log.php"></a></div> -->
            </div>
        
        
        <div class="unita_middle_content_1">
        <?php
	    if($device_id) {
	    $componentResult = $dbObj->getRecords("tbl_components", "", array("device_id"=>$device_id), "", "");
	    $color_key_msg = array();
	    $color_key_msg['green'] = "online";
	    $color_key_msg['yellow'] = "issue";
	    $color_key_msg['red'] = "attention";
	    $color_key_msg['blue'] = "NA";
		
	    while($row_component = mysql_fetch_array($componentResult)) { 
		if($row_component['component_name']=='CPUH'){
		$link = "component_CPU.php?cid=".$row_component['id']."&did=".$device_id;
		}else if($row_component['component_name']=='CPUV'){
		$link = "component_CPUV.php?cid=".$row_component['id']."&did=".$device_id;
		}else if($row_component['component_name']=='Displays'){
		$link = "component_display.php?cid=".$row_component['id']."&did=".$device_id;
		}else if($row_component['component_name']=='HVAC-H' || $row_component['component_name']=='HVAC-V'){
		$link = "component_HVAC.php?cid=".$row_component['id']."&did=".$device_id;
		}

//////////Color Code Query		
		
		
		$color_state = array();
		if($row_component['component_name']=='Displays'){

			$query = "select tbl_component_state.state from tbl_component_state, tbl_components where tbl_component_state.component_id=tbl_components.id and  component_name = 'Displays' and component_id='".$row_component['id']."'  order by tbl_component_state.id DESC";
			$exe_query = mysql_query($query);
			$test_color = array();
			$test_state = "";
			$i=1;
			$avgOfFive = array();
			//echo mysql_num_rows($exe_query);

			while($row_display = mysql_fetch_array($exe_query)){				
					
					$test_color[] = $row_display['state'];					
					
					if($i%5==0){
						
						//print_r($test_color);
						if(in_array("red", $test_color)){
							$test_state  = "red";
						}elseif(in_array("yellow", $test_color)){
							$test_state  = "yellow";
						}elseif(in_array("green", $test_color)){
							$test_state  = "green";
						}
						
						$avgOfFive[]= $test_state;							
						
						if((count($avgOfFive)-1)%3==0 ){
								//echo $avgOfFive[count($avgOfFive)-1];									
							$color_state[] = $avgOfFive[count($avgOfFive)-1];
							
						}	
						$test_color = "";								
					}					
		   		$i++;
			}
			//$color_state = array_reverse($color_state);
			//print_r($test_color);				
   
		}else{
			$qry = "select * from tbl_component_state where component_id='".$row_component['id']."' order by id DESC limit 27";
				//echo "<pre>";print_r($qry);exit;
				$result = mysql_query($qry);
				$total = mysql_num_rows($result);	
				$x = 0;
		
		
				while($row=mysql_fetch_array($result))
				{
					
					if($x%3 == 0) {			    
						$color_state[] = $row['state'];												
					}			
					$x++;
				}
		}
		
	?>
			
	    	<div class="unita_middle_row" style="width:auto;">
            		<div class="unita_progress_bar">
		<?php
		for($t=0; $t<9; $t++) { 
			
			if(isset($color_state[$t])) {
				$strip_color = $color_state[$t];					
			} else {
				$strip_color = "blue";
			}
			if($t==0) {
				$className = "big_strips";
				$msg = $color_key_msg[$strip_color];
			} else {
				$className = "small_strips";
				$msg = "";
			}
			?>
			
			<div id="s" class="<?php echo $className; ?>" style="background-color:<?php echo $strip_color; ?>"><?php echo $msg; ?></div>
			
		<?php } ?>
		</div>


                	<div class="unita_middle_txt" style="width:90px;"><?php echo $row_component['component_name']; ?></div>
                	<div class="unita_middle_arrow"><a href="<?=$link?>"></a> </div>
            	</div>
        	
	<?php } } ?>
        </div>
        
        <div class="unita_showtell_row">
        	<div class="unita_showtell_img"></div>
        </div>
        
        <div class="unita_bottm_menu">
        	<?php include("footer.php");?>
        </div>
        
        
    </div>
</div>


</body>
</php>
